package cn.broadsense.web.controller.ue;

import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.Channel3dData;
import cn.broadsense.biz.domain.ChannelTagDetails;
import cn.broadsense.biz.dto.Channel3dDataDto;
import cn.broadsense.biz.mapper.ChannelMapper;
import cn.broadsense.biz.service.Channel3dDataService;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.biz.service.ChannelTagDetailsService;
import cn.broadsense.biz.service.ChannelTagService;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.utils.bean.BeanUtils;
import cn.broadsense.thrid.api.DaHuaApiRepository;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Api(tags = "UE派出所试点数据接口")
@RequestMapping("/ue/channel")
@RestController
public class UEChannelController extends BaseController {


    @Autowired
    private ChannelService channelService;
    @Autowired
    private Channel3dDataService channel3dDataService;
    @Autowired
    private ChannelTagDetailsService channelTagDetailsService;

    @Autowired
    private ChannelTagService channelTagService;
    @Autowired
    private DaHuaApiRepository daHuaApiRepository;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询点位数据列表
     * @return
     */
    @ApiOperation("查询UE程序点位数据列表")
    @GetMapping("/list")
    public R<List<Channel>> queryUEChannels(){
        Channel channel = new Channel();
        channel.setFollow(1);
        channel.setThreeDDataStatus(2);
        channel.setIsPage(false);
        channel.setUserId(1l);
        List<Channel> channels =channelMapper.findList(channel);
        for (Channel details : channels) {
            try {
                Channel3dData channel3dData = channel3dDataService.getByChannelId(details.getId());
                if (channel3dData != null) {
                    Channel3dDataDto dto = new Channel3dDataDto();
                    BeanUtils.copyBeanProp(dto, channel3dData);
                    details.setChannel3dData(dto);
                }
                List<ChannelTagDetails> tagDetails = channelTagDetailsService.listByChannelId(details.getId());
                if (CollUtil.isNotEmpty(tagDetails)) {
                    Set<Long> collect = tagDetails.stream().map(ChannelTagDetails::getTagId).collect(Collectors.toSet());
                    details.setTagList(CollUtil.newArrayList(collect));
                    details.setChannelTagList(channelTagService.listByIds(details.getTagList()));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return R.ok(channels);
    }

    @ApiOperation("获取点位播放地址")
    @GetMapping("/play/{channelCode}")
    public R playChannel(@PathVariable String channelCode) {
        JSONObject play = daHuaApiRepository.play(channelCode);
        if(play!=null && play.containsKey("url")){
            String url=play.getString("url");
            play.put("url",replacePlayUrl(url));
            play.put("channelCode",channelCode);
        }
        return R.ok(play);
    }

    /**
     * 替换播放URL
     * @param url
     * @return
     */
    private String replacePlayUrl(String url){
        if(url!=null){
            url=url.replace("http://10.1.0.40:8050/cam/","http://175.10.38.166:8086/cam/");
            url=url.replace("http://10.1.0.41:8050/cam/","http://175.10.38.166:8086/cam1/");
            url=url.replace("http://10.1.0.42:8050/cam/","http://175.10.38.166:8086/cam2/");
            url=url.replace("http://10.1.0.43:8050/cam/","http://175.10.38.166:8086/cam3/");
            url=url.replace("http://10.1.0.44:8050/cam/","http://175.10.38.166:8086/cam4/");
        }
        return url;
    }

}
